/**
 * 对接lcd12864.c 实现游戏显示功能
 */
#include "lcd12864.c"
#include "game.h"

void display_init()
{
    lcd_init();
    lcd_clear_screan();
}

void display_game_start_screen()
{
    lcd_clear_screan();
    lcd_display_words_in_page(2, 0, TETRIS_PIC, 128 * 32 / 8, 128, 32);
}

void display_game_over_screen()
{
    lcd_clear_screan();
    lcd_display_words_in_page(0, 0, GAME_OVER_PIC, 128 * 64 / 8, 128, 64);
}

/**
 * 屏幕指定行和列，显示数字x
 */
void display_num(unsigned char page,
                 unsigned char col,
                 unsigned char num)
{
    // NUM_CHARS + num * 4  对应num在数组中元素的第一个byte的位置
    lcd_display_bytes_in_page(page, col, NUM_CHARS + num * 4, 4);
}

void display_speed_in_selection()
{
    display_num(4, 62, game_setting.speed);
}

/**
 * 游戏速度画面展示
 */
void display_speed_screen()
{
    lcd_clear_screan();
    lcd_display_same_bytes_in_page(0, 0, 0xFF, 128);
    lcd_display_same_bytes_in_page(7, 0, 0xFF, 128);
    // sizeof(SPEED_CHARS) / sizeof(SPEED_CHARS[0])
    // 对于char 数组 等价于
    lcd_display_bytes_in_page(3, 52, SPEED_CHARS, sizeof(SPEED_CHARS));
    lcd_display_bytes_in_page(4, 57, LEFT_ARROW, sizeof(LEFT_ARROW));
    lcd_display_bytes_in_page(4, 67, RIGHT_ARROW, sizeof(RIGHT_ARROW));
    display_speed_in_selection();
}

void display_score()
{
    unsigned char col = 3;
    display_num(1, col, game_setting.score / 10000);
    display_num(1, col += 5, (game_setting.score % 10000) / 1000);
    display_num(1, col += 5, (game_setting.score % 1000) / 100);
    display_num(1, col += 5, (game_setting.score % 100) / 10);
    display_num(1, col += 5, (game_setting.score % 10));
}

void display_speed_in_game()
{
    display_num(4, 13, game_setting.speed);
}

void display_game_screen()
{
    unsigned char i;
    lcd_clear_screan();
    // SCORE
    lcd_display_bytes_in_page(0, 3, SCORE_CHARS, sizeof(SCORE_CHARS));
    // SPEED
    lcd_display_bytes_in_page(3, 3, SPEED_CHARS, sizeof(SPEED_CHARS));

    // 游戏区的两条竖线
    for (i = 0; i < 8; i++)
    {
        lcd_display_one_byte_in_page(i, 30, 0xFF);
        lcd_display_one_byte_in_page(i, 71, 0xFF);
    }

    // 右侧区域数据
    lcd_display_words_in_page(1, 85, LOGO_WORDS_CH,
                              sizeof(LOGO_WORDS_CH) / sizeof(LOGO_WORDS_CH[0]),
                              16, 16);
    lcd_display_words_in_page(3, 77, LOGO_WORDS_EN,
                              sizeof(LOGO_WORDS_EN) / sizeof(LOGO_WORDS_EN[0]),
                              8, 16);

    // 显示数字
    display_score();
    display_speed_in_game();
}

void display_pause()
{
    lcd_display_words_in_page(6, 81, PAUSE_CHARS,
                              sizeof(PAUSE_CHARS) / sizeof(PAUSE_CHARS[0]),
                              8, 16);
}

void display_pause_hide()
{
    lcd_display_same_bytes_in_page(6, 81, 0x00, 40);
    lcd_display_same_bytes_in_page(7, 81, 0x00, 40);
}

void fill_display_block_arr(unsigned char top,
                            unsigned char bottom,
                            unsigned char display_byte[])
{
    unsigned char temp = 0x00;
    if (top == 1)
    {
        temp |= 0x0F;
    }

    if (bottom == 1)
    {
        temp |= 0xF0;
    }

    display_byte[0] = temp;
    display_byte[1] = temp & 0xBB;
    display_byte[2] = temp & 0xDD;
    display_byte[3] = temp;
}

/**
 * 在屏幕的任意一个位置写入shape数据
 */
void display_shape(unsigned char page,
                   unsigned char col,
                   unsigned int shape)
{
    // shape数据 分成4组，每组4位
    // 高8位  显示在page
    // 15 14 13 12
    // 11 10 09 08
    // 转换成像素数据
    // 15 + 11 = 4byte像素数据

    // 低8位  显示在page+1
    // 7 6 5 4
    // 3 2 1 0

    // 上下两个存储数据格子一组，转换成4byte的显示像素数据
    unsigned char display_byte[] = {0, 0, 0, 0};
    // 0 1 0 1
    // 0 0 1 1
    // 0+0 => 0,0,0,0
    // 1+0 => 0F,0D,0B,0F
    // 0+1 => F0,D0,B0,F0
    // 1+1 => FF,DD,BB,FF
    unsigned char top;
    unsigned char bottom;
    unsigned char i;
    for (i = 0; i < 4; i++)
    {
        top = ((shape >> (15 - i)) & 1) == 1;
        bottom = ((shape >> (11 - i)) & 1) == 1;

        fill_display_block_arr(top, bottom, display_byte);
        lcd_display_bytes_in_page(page, col + 4 * i, display_byte, 4);

        top = ((shape >> (7 - i)) & 1) == 1;
        bottom = ((shape >> (3 - i)) & 1) == 1;

        fill_display_block_arr(top, bottom, display_byte);
        lcd_display_bytes_in_page(page + 1, col + 4 * i, display_byte, 4);
    }
}

/**
 * GAME_ZONE 里面所有的数据显示到屏幕上
 */
void display_refresh_game_zone()
{
    // 游戏区行数是16行，每两行一起进行显示
    unsigned char row;
    unsigned char col;
    unsigned char top;
    unsigned char bottom;
    unsigned char display_bytes[4];
    // 每两行一起处理，一共需要处理8次
    for (row = 0; row < 8; row++)
    {
        // 一共处理10列
        for (col = 0; col < 10; col++)
        {
            // 第一行
            // 存储的数据，最左边的一位是第12位
            top = (((GAME_ZONE[row * 2] >> (12 - col)) & 1) == 1);
            // 第二行
            bottom = (((GAME_ZONE[row * 2 + 1] >> (12 - col)) & 1) == 1);
            fill_display_block_arr(top, bottom, display_bytes);
            lcd_display_bytes_in_page(row, 31 + col * 4, display_bytes, 4);
        }
    }
}

void display_next_shape()
{
    display_shape(6, 8, game_shape.next_shape);
}